<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ attribute name="serverType" type="java.lang.String" required="true" description="云服务类型"%>
<%@ attribute name="uploadPath" type="java.lang.String" required="false" description="文件根目录"%>
<%@ attribute name="callback" type="java.lang.String" required="false" description="回调函数"%>
<%@ attribute name="fileType" type="java.lang.String" required="false" description="文件类型"%>
<%@ attribute name="fileSize" type="java.lang.String" required="false" description="文件大小KB"%>
<%@ attribute name="isMulti" type="java.lang.String" required="false" description="是否批量上传"%>

<%@ attribute name="command" type="java.lang.String" required="false" description="上传方法"%>
<%@ attribute name="input" type="java.lang.String" required="true" description="输入框"%>
<%@ attribute name="lessonSn" type="java.lang.String" required="false" description="lessonSn"%>
<%@ attribute name="hasCheckSn" type="java.lang.String" required="false" description="hasCheckSn LessonSn"%>

<ol id="${input}Preview"></ol>
<a href="javascript:" onclick="${input}FileWindowOpen();" class="btn">${selectMultiple?'添加':'选择'}</a>
&nbsp;<a href="javascript:" onclick="${input}DelAll();" class="btn">清除</a>

<script type="text/javascript">

function ${input}FileWindowOpen(){
	var date = new Date(), year = date.getFullYear(), month = (date.getMonth()+1)>9?date.getMonth()+1:"0"+(date.getMonth()+1);
	var uploadWinUrl = '${ctxStatic}/file-tool/uploadify/upload/docUploadify.jsp';
	var uploadAction  = "${ctxStatic}/file/core/connector/java/connector.java;JSESSIONID=${pageContext.session.id}"; //支持多参数上传
	var fileTypeDesc = '所有文件';
	var callback = '${callback}';
	if(callback == ''){
		callback = '${input}SelectAction';
	}
	uploadWinUrl = addUrlParam(uploadWinUrl,'callback',callback);
	uploadWinUrl = addUrlParam(uploadWinUrl,'serverType','${serverType}');
	uploadWinUrl = addUrlParam(uploadWinUrl,'isMulti','${isMulti}');
	uploadWinUrl = addUrlParam(uploadWinUrl,'fileType','${fileType}');
	uploadWinUrl = addUrlParam(uploadWinUrl,'uploadPath','${uploadPath}');
	uploadWinUrl = addUrlParam(uploadWinUrl,'fileSize','${fileSize}');
	uploadWinUrl = addUrlParam(uploadWinUrl,'command','${command}');
	uploadWinUrl = addUrlParam(uploadWinUrl,'lessonSn','${lessonSn}');
	uploadWinUrl = addUrlParam(uploadWinUrl,'hasCheckSn','${hasCheckSn}');
	
	var title = "附件上传";
	var dialogOpts = { width : 400,height: 370,modal: false,buttons: { }}; 
	
	openWindows(dialogOpts,uploadWinUrl,null,'dialogId',title);
	
}

function ${input}SelectAction(ids){
	
	var old = $("#${input}").val();
	var newIds = old+","+ids;
	var files = newIds.split(",");
	var url = [];
	for(var i=0; i<files.length; i++){
		if(files[i]!='' && files[i]!=null){
			url.push(files[i]);
		}
	}
	$("#${input}").val(url.join());
	${input}Preview();
}

function ${input}Del(obj){
	var url = $(obj).prev().attr("url");
	$("#${input}").val($("#${input}").val().replace("|"+url,"","").replace(url+"|","","").replace(url,"",""));
	${input}Preview();
}
function ${input}DelAll(){
	$("#${input}").val("");
	${input}Preview();
}

function ${input}Preview(){
	var idValue = $("#${input}").val();
	idValue = jQuery.trim(idValue);
	var li, urls = idValue.split(",");
	$("#${input}Preview").children().remove();
	var url='';
	var files = null;
	//<c:if test="${serverType eq 'dby'}">
	url='${fns:getConfig("dby.fileUri")}'+'?u=';
	files = get${input}FileInfo('dby',idValue);
	//</c:if>
	//<c:if test="${serverType eq 'xdy'}">
	files = get${input}FileInfo('xdy',idValue);
	//</c:if>
	//<c:if test="${serverType eq 'local'}">
	url='${pageContext.request.contextPath}/file';
	//</c:if>
	//<c:if test="${serverType eq 'vky'}">
	files = get${input}FileInfo('vky',idValue);
	//</c:if>
	for (var i=0; i<urls.length; i++){
		if (urls[i]!=""){
			var fileId = urls[i];
			var links = url+fileId;
			if(url == ''){
				links = 'javascript:void(0);';
			}
			var showName = decodeURIComponent(fileId.substring(fileId.lastIndexOf("/")+1));
			var showDate = "";
			var showSize = "";
			var showIcon = '${ctxStatic}/images/doc/file_extension_pdf.png';
			if(files!=null){
				var file = files[fileId];
				if(file != null){
					showName = file.name;
					showDate = '&nbsp;&nbsp;&nbsp;'+file.showDate+'';
					showSize = '&nbsp;&nbsp;&nbsp;'+file.showSize+'';
					if(file.url!= null && file.url!=''){
						links = file.url;
					}
				}
			}
			li = '<li>';
			//li += '<img class="showIcon" src="'+showIcon+'">';
			li += "<a href=\""+links+"\" url=\""+fileId+"\" target=\"_blank\">"+showName+"</a>";
			li += ''+showDate+showSize;
			li += "&nbsp;&nbsp;<c:if test="${!readonly}"><a href=\"javascript:\" onclick=\"${input}Del(this);\" title=\"删除\">×</a></c:if></li>";
			$("#${input}Preview").append(li);
		}
	}
	if ($("#${input}Preview").text() == ""){
		$("#${input}Preview").html("<li style='list-style:none;padding-top:5px;'>无</li>");
	}
}

function get${input}FileInfo(serverType,ids){
	var url = "${ctx}/doc/list";
	var data = {serverType: serverType, ids : ids};
	var map = {};
    jQuery.ajax({  
		url: url, async : false,type: "POST",
		data : data,
		success: function(response) {
			list = response;
			for (var i=0; i<list.length; i++){
				var file = list[i];
				map[file.uid]=file;
			}
		},
		error: function(e){console.error(e);}
	});
    return map;
}
${input}Preview();


</script>